<template>
  <div class="sensor" v-loading="loading">
    <template v-if="list.length > 0" v-for="item in list" :key="item">
      <div class="sensor-item" @click="handleClick(item)">
        <div class="sensor-item-name">
          <div>
            <span class="sensor-item-name-text">{{ item.areaLocation }}</span>
            <span>&nbsp;&nbsp;·&nbsp;&nbsp;</span>
            <span>{{ item.sensorTypeName }}</span>
          </div>
        </div>
        <div class="sensor-item-location">
          <span>{{ item.areaName }}</span>
          <div>
            <span class="sensor-item-value">{{ item.showValue.replace(item.unitCode, '') || '--' }}</span>
            <span class="sensor-item-value-unit">{{ item.unitCode }}</span>
          </div>
        </div>
        <div class="sensor-item-time">
          <span>工作时长 {{ item.keepTimeStr }}</span>
        </div>

        <div v-for="alarm in item.alarmList" :key="alarm.id" class="alarm" @click.stop="handleAlarmClick(item, alarm)">
          <span>{{ alarm.alarmTypeName }}</span>
          <span>详情</span>
        </div>
      </div>
    </template>
    <b-empty v-else></b-empty>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { getSensorList } from '@/api/sensor'
import bEmpty from '@/components/b-empty.vue'

const props = defineProps({
  params: {
    type: Object
  }
})

const interval = ref(null)
onMounted(() => {
  getSensorListFun()

  interval.value = setInterval(() => {
    getSensorListFun()
  }, 60000)
})

onUnmounted(() => {
  clearInterval(interval.value)
})

const loading = ref(false)
const list = ref([])
async function getSensorListFun() {
  loading.value = true
  const { data } = await getSensorList({ ...props.params, pageSize: -1 })
  list.value = data.records
  loading.value = false
}
</script>
<style scoped lang="scss">
.sensor {
  width: 100%;
  min-width: 616px;
  display: flex;
  flex-wrap: wrap;
  gap: calc(8px * var(--scale));
  .sensor-item {
    width: 300px;
    display: flex;
    flex-direction: column;
    padding: 8px;
    background: var(--bg-color-page);
    gap: 4px;
    cursor: pointer;
    direction: ltr;
    .sensor-item-name {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 14px;
      .sensor-item-name-text {
        color: var(--el-color-primary);
      }

    }
    .sensor-item-location {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 4px;
      font-size: 12px;
      color: var(--text-color-disabled);
       .sensor-item-value {
        font-size: 14px;
        color: var(--el-color-primary);
      }
      .sensor-item-value-unit {
        margin-left: 2px;
        font-size: 12px;
        color: var(--text-color-placeholder);
      }
    }
    .sensor-item-time {
      margin-top: 8px;
      font-size: 12px;
      color: var(--text-color-placeholder);
    }
    .alarm {
      margin-top: 8px;
      font-size: 12px;
      color: #ff0000;
      background: rgba(255, 0, 0, 0.23);
      padding: 8px 12px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
</style>
